package io.github.uicdb.liba.api.register;

import net.minecraft.client.model.geom.ModelLayerLocation;
import net.minecraft.client.model.geom.builders.LayerDefinition;
import net.minecraft.client.renderer.entity.EntityRendererProvider;
import net.minecraft.client.renderer.entity.EntityRenderers;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.world.entity.Entity;
import net.minecraft.world.entity.EntityType;
import net.minecraft.world.entity.MobCategory;
import net.minecraftforge.api.distmarker.Dist;
import net.minecraftforge.fml.loading.FMLEnvironment;

import java.util.HashMap;
import java.util.Map;
import java.util.function.Consumer;
import java.util.function.Supplier;

public class EntityTypeBuilder <T extends Entity> {
    protected final LazyRegistry lazyRegistry;
    protected final String name;
    protected final EntityType.EntityFactory<T> factory;
    protected MobCategory mobCategory=MobCategory.MISC;
    protected EntityTypeHolder<T> result=new EntityTypeHolder<>();
    protected Consumer<EntityType.Builder<T>> typeBuilderConsumer= tBuilder -> tBuilder.sized(1, 1);
    private Supplier<Supplier<EntityRendererProvider>> rendererSupplier;
    private Map<String,Supplier<Supplier<LayerDefinition>>> layers=new HashMap<>();
    private final Map<String,String> langMap=new HashMap<>();
    public EntityTypeBuilder(LazyRegistry lazyRegistry, String name, EntityType.EntityFactory<T> factory) {
        this.lazyRegistry = lazyRegistry;
        this.name = name;
        this.factory = factory;
    }
    public EntityTypeBuilder<T> attribute(Consumer<EntityType.Builder<T>> typeBuilderConsumer){
        this.typeBuilderConsumer=typeBuilderConsumer;
        return this;
    }
    public EntityTypeBuilder<T> category(MobCategory category){
        this.mobCategory=category;
        return this;
    }
    public EntityTypeBuilder<T> render(Supplier<Supplier<EntityRendererProvider>> rendererSupplier){
        this.rendererSupplier=rendererSupplier;
        return this;
    }
    public EntityTypeBuilder<T> layer(String name,Supplier<Supplier<LayerDefinition>> layer){
        this.layers.put(name,layer);
        return this;
    }
    public EntityTypeBuilder<T> lang(String lang,String text){
        langMap.put(lang,text);
        return this;
    }
    public EntityTypeHolder<T> build(){
        result.type=lazyRegistry.entityTypes.register(name,()-> {
           var builder=EntityType.Builder.of(factory, mobCategory);
           typeBuilderConsumer.accept(builder);
          return builder.build(lazyRegistry.modid+":"+name);
        });
        if(FMLEnvironment.dist== Dist.CLIENT){
            if(rendererSupplier!=null)
            lazyRegistry.executeWhenClientSetup(() -> EntityRenderers.register(result.get(),rendererSupplier.get().get()));
            layers.forEach((s, supplierSupplier) -> lazyRegistry.entityLayerMap.put(() -> () -> new ModelLayerLocation(new ResourceLocation(lazyRegistry.modid, name), s), supplierSupplier));
        }
        if(!langMap.containsKey("en_us"))
            lang("en_us",LangBuilder.toTitleCase(name));
        langMap.forEach((lang,test)->lazyRegistry.lang(lang,()->new TranslationBean(result.get().getDescriptionId(),LangBuilder.toTitleCase(name))));
        return result;
    }

}
